from django.test import TestCase
from sign.models import Event, Guest
from django.contrib.auth.models import User


# Create your tests here.

class ModelTest(TestCase):
    def setUp(self) -> None:
        Event.objects.create(id=1, name='3 of event', limit=2000, status=True, address='shenzheng',
                             start_time='2021-6-11 02:41:41')
        Guest.objects.create(id=1, event_id=1, realname='tt', phone='1311210010', email='tt@qq.com', sign=False)

    def test_event_models(self):
        result = Event.objects.get(name='3 of event')
        self.assertEqual(result.address, 'shenzheng')
        self.assertTrue(result.status)

    def test_guest_model(self):
        result = Guest.objects.get(event_id=1)
        self.assertEqual(result.phone, '1311210010')
        self.assertFalse(result.sign)


class IndexPageTest(TestCase):  # 测试index登陆首页
    def test_index_page_renders_index_template(self):
        response = self.client.get('/index/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'index.html')
  

class LoginActinTest(TestCase):#测试登陆动作
    def setUp(self) -> None:
        User.objects.create_user('admin', 'admin@mail.com', 'admin123456')

    def test_add_admin(self):#测试添加用户
        user = User.objects.get(username='admin')
        self.assertEqual(user.username,'admin')
        self.assertEqual(user.email,'admin@mail.com')

    def test_kogin_action_username_password_null(self):#用户名密码为空
        test_data = {'username':'', 'password':''}
        response = self.client.post('/login_action/',data=test_data)
        self.assertEqual(response.status_code,200)
        self.assertIn(b'username or password error!', response.content)